-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
62 lines (52 loc) · 1.2 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <stdio.h>
#include <string.h>
typedef struct
{
int chave;
char valor[21];
} Map;
void BubbleSort(Map *mapa, int pTam);
int main()
{
int n, v;
char s[21];
while (scanf("%d", &n) != EOF)
{
Map mapa[n];
for (int i = 0; i < n; i++)
{
scanf("%s %d", s, &v);
strcpy(mapa[i].valor, s);
mapa[i].chave = v;
}
BubbleSort(mapa, n);
for (int i = 0; i < n; i++)
{
if (i < n - 1)
printf("%s ", mapa[i].valor);
else
printf("%s\n", mapa[i].valor);
}
}
return 0;
}
void BubbleSort(Map *mapa, int pTam)
{
int i, Trocou;
Map aux;
do
{
Trocou = 0;
for (i = 0; i < pTam - 1; i++)
if (mapa[i].chave > mapa[i + 1].chave)
{
aux.chave = mapa[i].chave;
strcpy(aux.valor, mapa[i].valor);
mapa[i].chave = mapa[i + 1].chave;
strcpy(mapa[i].valor, mapa[i + 1].valor);
mapa[i + 1].chave = aux.chave;
strcpy(mapa[i + 1].valor, aux.valor);
Trocou = 1;
}
} while (Trocou);
}